package com.tensai.study.java.newconcept.typereactiveprogram.reactor.disposable;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import reactor.core.Disposable;
import reactor.core.Disposables;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * Disposable.Composite可以将多个Disposable合并起来，在后面统一做处理。
 *
 * @author Tensai
 */
@Slf4j
public class CompositeTest {

	@Test
	public void test() throws InterruptedException {
		Disposable disposable1 = Flux.interval(Duration.ofMillis(100)).map(Object::toString).subscribe(log::info);
		Disposable disposable2 = Flux.interval(Duration.ofMillis(200)).map(Object::toString).subscribe(log::info);
		Disposable disposable3 = Flux.interval(Duration.ofMillis(300)).map(Object::toString).subscribe(log::info);

		Disposable.Composite composite = Disposables.composite();
		composite.add(disposable1);
		composite.add(disposable2);
		composite.add(disposable3);

		TimeUnit.SECONDS.sleep(2);
		composite.isDisposed();
	}
}
